/*
 *  Class name: ArrangeableEntityTableHandler.java
 *  Version: 1.0
 *  Date: 26.2.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.entity.handlers.table;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import org.richfaces.component.SortOrder;

import static javax.faces.context.FacesContext.getCurrentInstance;

/**
 *
 * @author Michal Ďuriš
 */
abstract public class ArrangeableEntityTableHandler {

    protected final Map<String, SortOrder> sortOrders = new HashMap<>();
    protected final Map<String, String> filterValues = new HashMap<>();

    private String sortProperty;

    public ArrangeableEntityTableHandler() {
    }

    public abstract Object getDataModel();

    public Map<String, SortOrder> getSortOrders() {
        return sortOrders;
    }

    public Map<String, String> getFilterValues() {
        return filterValues;
    }

    public String getSortProperty() {
        return sortProperty;
    }

    public void setSortProperty(String sortProperty) {
        this.sortProperty = sortProperty;
    }

    public void toggleSort() {

        if (sortProperty != null && !sortProperty.isEmpty()) {

            for (Map.Entry<String, SortOrder> entry : sortOrders.entrySet()) {

                SortOrder newOrder;

                if (entry.getKey().equals(sortProperty)) {

                    if (entry.getValue() == SortOrder.ascending) {
                        newOrder = SortOrder.descending;
                    } else {
                        newOrder = SortOrder.ascending;
                    }
                } else {
                    newOrder = SortOrder.unsorted;
                }

                entry.setValue(newOrder);
            }

        } else {
            //@TODO: Replace with logger
            System.err.println("sortProperty is null or empty...");
        }

//        if(!sortOrders.isEmpty()) {
//            
//            if(sortProperty != null && !sortProperty.isEmpty()) {
//                
//                if(sortOrders.containsKey(sortProperty)) {
//                    
//                    SortOrder actualSortOrder = sortOrders.get(sortProperty);
//                    
//                    switch(actualSortOrder) {
//                        case ascending:
//                            actualSortOrder = SortOrder.descending;
//                            break;
//                        case descending: 
//                            actualSortOrder = SortOrder.unsorted;
//                            break;
//                        case unsorted:
//                            actualSortOrder = SortOrder.ascending;
//                            break;
//                        default:
//                            throw new IllegalArgumentException(getClass().getName() 
//                                                               + ": Illegal sort order.");
//                    }
//                    
//                    sortOrders.put(sortProperty, actualSortOrder);
//                    
//                } else {
//                    System.out.println(getClass().getName() 
//                               + ": Sort property not found in map!");
//                }
//                
//            } else {
//                System.out.println(getClass().getName() 
//                               + ": Sort property not set!");
//            }
//            
//            
//        } else {
//            //TODO: Logger
//            System.out.println(getClass().getName() 
//                               + ": Trying to set sort order of the property, but"
//                               + "the sortOrders map is empty!");
//        }
    }

    public String getLocalizedSortOrderText(String propertyName) {
        ResourceBundle bundle = getCurrentInstance()
                .getApplication().getResourceBundle(
                        getCurrentInstance(), "bundle");

        switch (sortOrders.get(propertyName)) {
            case ascending:
                return bundle.getString("columnSortingLinkASC");
            case descending:
                return bundle.getString("columnSortingLinkDESC");
            case unsorted:
                return bundle.getString("columnSortingLinkNoSorting");
            default:
                return null;
        }
    }

}
